package com.gitee.qdbp.base.service;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import com.gitee.qdbp.base.system.service.IPasswordService;

/**
 * 密码服务实现类
 *
 * @author zhaohuihua
 * @version 151010
 */
// @Service
public class PasswordService implements IPasswordService {

    /** 日志对象 **/
    // private static final Logger log = LoggerFactory.getLogger(PasswordService.class);

    @Autowired
    private HashedCredentialsMatcher hashedCredentialsMatcher;

    @Override
    public String create(String id, String password) {
        String algorithm = hashedCredentialsMatcher.getHashAlgorithmName();
        int iterations = hashedCredentialsMatcher.getHashIterations();
        // String salt = PasswordShiro.salt(id);
        String salt = null;
        SimpleHash hash = new SimpleHash(algorithm, password, salt, iterations);
        return hash.toHex();
    }

    @Override
    public boolean validate(String id, String password, String hashPassword) {
        return create(id, password).equalsIgnoreCase(hashPassword);
    }
}
